﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Windows;
using D.Net.EmailClient;
using D.Net.EmailInterfaces;

namespace ManagerClient.Messaging
{
    public class MailProvider
    {
        private int port;
        private string smtpAdress;

        public void SendMessage(MailInfo mailInfo)
        {
            switch (mailInfo.Credentials.ClientlientType)
            {
                case SmtpClientType.Google:
                    {
                        smtpAdress = "smtp.gmail.com";
                        port = 587;
                        break;
                    }
                case SmtpClientType.MailRu:
                    {
                        throw new NotImplementedException();
                    }
            }
            var client = new SmtpClient(smtpAdress, port)
                {
                    Credentials = new NetworkCredential(mailInfo.Credentials.Login, mailInfo.Credentials.Password),
                    EnableSsl = true
                };

            var outgoingMessage = new MailMessage();
            outgoingMessage.From = new MailAddress(mailInfo.Credentials.Login);
            outgoingMessage.To.Add(new MailAddress(mailInfo.To));
            outgoingMessage.Subject = mailInfo.Subject;
            outgoingMessage.Body = mailInfo.Body;
            try
            {
                //   Прикрепляем файл
                string file = Path.Combine("D:\\ДИПЛОМ", "Dropbox", "исходные данные.xlsx");
                var attach = new Attachment(file, MediaTypeNames.Application.Octet);
                // Добавляем информацию для файла
                ContentDisposition disposition = attach.ContentDisposition;
                disposition.CreationDate = File.GetCreationTime(file);
                disposition.ModificationDate = File.GetLastWriteTime(file);
                disposition.ReadDate = File.GetLastAccessTime(file);
                outgoingMessage.Attachments.Add(attach);
                client.Send(outgoingMessage);
            }
            catch (SmtpException e)
            {
                MessageBox.Show("Возникла ошибка при отправке сообщения. Пожалуйста, попробуйте позже");
            }
        }

        public IEnumerable<IEmail> ReceiveMessagesFromProvider(MailInfo mailInfo)
        {
            IEmailClient ImapClient = EmailClientFactory.GetClient(EmailClientEnum.IMAP);
            ImapClient.Connect("imap.gmail.com", mailInfo.Credentials.Login, mailInfo.Credentials.Password, 993, true);
            ImapClient.SetCurrentFolder("INBOX");
            ImapClient.LoadRecentMessages(5);
            // Загрузить сообщения определенного пользователя
            IEnumerable<IEmail> messages =
                ImapClient.Messages.Select(m =>
                    {
                        m.LoadInfos();
                        return m;
                    });

            return messages;
        }
    }
}